<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layuimini-container">
    <form class="layui-form layui-form-pane" action="">
        <fieldset class="table-search-fieldset">
            <legend>卡券兑换金币比例</legend>
            <div style="margin: 10px 10px 10px 10px">

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">总比例:</label>
                        <div class="layui-input-inline">
                            <input type="text" name="proportion"  lay-verify="proportion" value="{$amount}%" autocomplete="off" class="layui-input">
                        </div>

                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-sm" lay-submit lay-filter="data-search-btn"  > 修改</button>
                    </div>
                    <span>例:100点券兑换成120金币 则填写1.2 自动换成百分数120%</span>
                </div>

            </div>
        </fieldset>
    </form>
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <script type="text/html" id="look">
            <button data-method="offset" data-type="auto" class="layui-btn " id="lookstyle" lay-event="look">查看</button>
        </script>

    </div>
</div>

{include file="public/footer" /}
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table;
        //自定义验证规则
        form.verify({
            proportion: function (value) {
                var a = value.indexOf('%');
                if (value == '' || a !=-1) {
                    //!=-1 是包含 %
                    return '卡券兑换金币比例不能为空 或者不能包含%';
                }
            },
        });

        table.render({
            elem: '#currentTableId',
            url: '/admin/voucher/index?active=ajax',
            method: 'post',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data, //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            //打印图标 导出图标
            defaultToolbar: ['filter'],
            cols: [[
                // {type: "checkbox",},
                // {field: 'id', title: 'ID'},
                {field: 'title', title: '卡券名称'},
                {field: 'price', title: '卡券金额'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            //修改卡券兑换金额比例
            $.ajax({
                url: '/admin/voucher/voucher_amount?amount_id={$amount_id}',
                type: 'post',
                dataType: 'json',
                data: data.field,
                success: function (data) {
                    if (data.status == true) {
                        parent.layer.msg("操作成功!", {icon:1,time: 1000}, function () {
                            //重新加载父页面
                            $("input[name='proportion']").val(data.proportion+'%');
                        });
                        return;
                    }
                },
                error: function () {
                    layer.msg("修改失败");
                }
            });

            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '80%'],
                    content: '/admin/voucher/voucher_add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = obj.data.id;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '80%'],
                    content: "/admin/voucher/voucher_write?id="+id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定要删除么?', function (index) {
                    //发异步删除数据
                    $.ajax({
                        url: '/admin/voucher/voucher_del',
                        type: 'post',
                        dataType: 'json',
                        data: {id: id},
                        success: function (data) {
                            if (data.status) {
                                // $(obj).parents("tr").remove();
                                layer.msg('删除成功!', {
                                    icon: 1,
                                    time: 1000
                                });
                                obj.del();

                            }
                        },
                        error: function () {
                            layer.msg("删除失败");
                        }
                    });
                    layer.close(index);
                });
            }else  if (obj.event === 'look') {
                var explain  =  data.explain
                //触发事件  查看内容
                var index = layer.open({
                    title:'查看内容',
                    type: 1,
                    area: ['700px', '450px'],
                    fixed: false, //不固定
                    shade:0.5,
                    shadeClose:true,
                    content: "<div align ='center'>"+explain+"</div>",
                });
            }
        });
    });
</script>

</body>
</html>